package com.medicine.hbp.fragment;

import java.util.Calendar;

import org.json.JSONException;
import org.json.JSONObject;
import org.kymjs.kjframe.http.HttpParams;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.medicine.hbp.R;
import com.medicine.hbp.adapter.CareAboutAdapter;
import com.medicine.hbp.http.AppHttpCallBack;
import com.medicine.hbp.http.AppHttps;
import com.medicine.hbp.http.HttpInterface;
import com.medicine.hbp.mob.findCareAboutListVo;
import com.medicine.hbp.sp.LoginSp;
import com.medicine.hbp.ui.AlarmRemindActivity;
import com.medicine.hbp.uitl.Share;
import com.medicine.hbp.uitl.StartIntent;
import com.medicine.hbp.uitl.ToastUtil;

/**
 * 参与-关心
 */
public class CareAboutFragment extends Fragment implements
		OnClickListener {
	private View view;
	private ListView listView;
	private CareAboutAdapter adapter;
	private LinearLayout layout_tixing;
	private LinearLayout layout_shuaxin;
	private Button btn_fasong;
	private EditText edit_content;
	private TextView data_time;
	private TextView no_login;

	@Override
	public android.view.View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.fragment_careabout, container, false);
		listView = (ListView) view.findViewById(R.id.xListView);
		layout_tixing = (LinearLayout) view.findViewById(R.id.layout_tixing);
		layout_shuaxin = (LinearLayout) view.findViewById(R.id.layout_shuaxin);
		btn_fasong = (Button) view.findViewById(R.id.btn_fasong);
		edit_content = (EditText) view.findViewById(R.id.edit_content);
		data_time = (TextView) view.findViewById(R.id.data_time);
		no_login = (TextView) view.findViewById(R.id.no_login);
		initView();
		return view;
	}

	private void initView() {
		adapter = new CareAboutAdapter(getActivity());
		listView.setAdapter(adapter);
		layout_tixing.setOnClickListener(this);
		layout_shuaxin.setOnClickListener(this);
		btn_fasong.setOnClickListener(this);
		edit_content.setFocusable(false);
		edit_content.setFocusableInTouchMode(false);
		no_login.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				StartIntent.startLogin(getActivity());
			}
		});
	}
	@Override
	public void onResume() {
		super.onResume();
		data_time.setText("今天是"+Calendar.getInstance().get(Calendar.YEAR)+"年"+(Calendar.getInstance().get(Calendar.MONTH)+1)+"月"+Calendar.getInstance().get(Calendar.DAY_OF_MONTH)+"日，下面内容适合给老年人长辈");
		if(TextUtils.isEmpty(LoginSp.getToken(getActivity()))){
			no_login.setVisibility(View.VISIBLE);
			listView.setVisibility(View.GONE);
		}else{
			no_login.setVisibility(View.GONE);
			listView.setVisibility(View.VISIBLE);
			http();
		}
		httpMsg();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.layout_tixing:// 设置提醒
				Intent intent = new Intent(getActivity(), AlarmRemindActivity.class);
				startActivity(intent);
				break;
			case R.id.layout_shuaxin:// 刷新内容
				httpMsg();
				break;
			case R.id.btn_fasong:// 发送关心
				if(TextUtils.isEmpty(LoginSp.getToken(getActivity()))){
					ToastUtil.showShort(getActivity(), "请先登录再发送健康关心");
					StartIntent.startLogin(getActivity());
				}else{
					Share.gxopenShare(getActivity(), edit_content.getText().toString(), edit_content.getText().toString(), "http://www.baidu.com");
				}
				break;
			default:
				break;
		}
	}

	/**
	 * 获取关心记录接口
	 */
	private void http() {
		HttpParams params = new HttpParams();
		AppHttps.HttpPost(getActivity(), HttpInterface.FIND_CARE_ABOUT_LIST,
				params, new AppHttpCallBack<findCareAboutListVo>(
						findCareAboutListVo.class, getActivity()) {
					@Override
					public void onSuccess(findCareAboutListVo result) {
						if(result.info!=null){
							adapter.setItems(result.info);
							adapter.notifyDataSetChanged();
						}
					}

					@Override
					public void onFailures(int errorCode, String errorMsg) {
						ToastUtil.showShort(getActivity(), errorMsg);
					}
				});

	}

	/**
	 * 获取关心内容接口
	 */
	private void httpMsg() {
		HttpParams params = new HttpParams();
		AppHttps.HttpPost(getActivity(), HttpInterface.FIND_CARE_ABOUT_DATA,
				params,
				new AppHttpCallBack<Object>(Object.class, getActivity()) {
					@Override
					public void onSuccess(String t) {
						super.onSuccess(t);
						try {
							JSONObject object = new JSONObject(t);
							JSONObject object1 = object
									.getJSONObject("careAboutDataInfo");
							edit_content.setText(object1
									.getString("careAboutData"));
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}

					@Override
					public void onSuccess(Object result) {
					}

					@Override
					public void onFailures(int errorCode, String errorMsg) {
						ToastUtil.showShort(getActivity(), errorMsg);
					}
				});

	}

}
